// miniprogram/pages/query/query.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  tozaoxue: function (e) {
    wx.navigateTo({
      url: '../out/gkbs/gkbs',
    })
    // wx.navigateToMiniProgram({
    //   appId: 'wxca86930ec3e80717', //模考appid
    //   path: 'page/course/details?fenxiang=1&id=220173', //模考
    //   envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
    //   success(res) {
    //     console.log('打开成功！')
    //   }
    // })
  },
  zkzInput: function (e) {
    this.setData({
      zkz: e.detail.value
    })
    console.log(this.data.zkz.length)
  },

  getPhoneNumber(e) {
    var _ = this;
    var zkz = _.data.zkz;
    var area = _.data.area;
    var uid = _.data.uid;
    var page = _.data.page;
    wx.setStorageSync('zkz', zkz)
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
      })
      console.log(res.result)
      console.log(area)
      console.log(uid)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 36,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.uid,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      if (zkz.length > 5) {
        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/chaxun/register/?actid=44526&callback=?",
          data: {
            phone: res.result,
            area: area,
            uid: uid,
            zkz: zkz
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson.lists)
            wx.setStorageSync('lists', theJson.lists)
            if (theJson.lists.length) {
              wx.navigateTo({
                url: '../query_result/query_result',
              })
            } else {
              _.setData({
                no: true
              })
            }
          }
        })
      } else {
        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/chaxun/register/?actid=44526&callback=?",
          data: {
            phone: res.result,
            area: area,
            uid: uid,
            name: zkz
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson.lists)
            wx.setStorageSync('lists', theJson.lists)
            if (theJson.lists.length) {
              wx.navigateTo({
                url: '../query_result/query_result',
              })
            } else {
              _.setData({
                no: true
              })
            }
          }
        })
      }


    }).catch(err => {
      console.error(err);
    });
  },

  closeno: function (options) {
    var _ = this;
    _.setData({
      no: false
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options)
    
    _.setData({
      area: options.area,
      uid: options.agent,
    })
    if(options.page){
      _.setData({
        area: options.area,
      uid: options.agent,
        page: options.page
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _ = this;
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})